import 'package:flutter/material.dart';

import '../../ble/protocol/AutoConnectUtil.dart';
import '../../ble/protocol/iledcolor/BleBean.dart';
import '../../ble/protocol/iledcolor/source/graffiti/GraffitiSourceBean.dart';
import '../../ble/protocol/iledcolor/source/text/TextSourceBean.dart';
import 'ShowGraffitiSourceWidget.dart';

class ShowTextSourceWidget extends StatefulWidget {
  final TextSourceBean sourceBean;

  const ShowTextSourceWidget({super.key, required this.sourceBean});

  @override
  State<ShowTextSourceWidget> createState() => _ShowTextSourceWidgetState();
}

class _ShowTextSourceWidgetState extends State<ShowTextSourceWidget> {
  GraffitiSourceBean? graffitiSourceBean;

  BleBean getBleBean() {
    var lastConnectBleBean = AutoConnectUtil.instance.getLastConnectBleBean();
    BleBean bleBean = BleBean(bean: lastConnectBleBean);
    return bleBean;
  }

  @override
  void initState() {
    super.initState();
    init();
  }

  void init() async {
    var cast2graffitiSourceBeans = await widget.sourceBean.cast2GraffitiSourceBeans(getBleBean());
    setState(() {
      graffitiSourceBean = cast2graffitiSourceBeans.first;
    });
  }

  @override
  Widget build(BuildContext context) {
    return graffitiSourceBean != null ? ShowGraffitiSourceWidget(sourceBean: graffitiSourceBean!) : const SizedBox();
  }
}
